package cn.yanxiaohui.service.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.yanxiaohui.common.vo.ActionResult;
import cn.yanxiaohui.data.entity.User;
import cn.yanxiaohui.service.web.service.UserService;

/**
 * 登录控制器
 * @author yanxh
 *
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController{
	
	Logger log = LoggerFactory.getLogger(LoginController.class);
	
	/** 视图统一前缀 **/
	private final static String PREFIX = "login/";
	
	@Autowired
	UserService userService;

	@RequestMapping
	public String toIndex(ModelMap model) {
		return index(model);
	}
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(ModelMap model) {
		return PREFIX + "index";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	@ResponseBody
	public ActionResult login(ModelMap model, String userName, String pwd, String code, String codeHidden) {
		log.info("登录用户信息：【userName="+userName+",pwd="+pwd+",code="+code+",codeHidden="+codeHidden+"】");
		if(StringUtils.isBlank(code) || !code.equalsIgnoreCase(codeHidden)) {
			return ActionResult.error("验证码有误");
		}
		
		User user = userService.userByUserName(userName);
		if(user == null) {
			return ActionResult.error("用户名不存在");
		}
		if(!user.getPassword().equals(pwd)) {
			return ActionResult.error("密码不正确");
		}
		
		return ActionResult.success("厉害了，我的哥");
	}
}
